<?php

require_once('jhtraq.php');
class JhtraqModelMilestones extends JhtraqModelJhtraq
{
	function get($id, $method = 'loadObject')
	{
		$this->db->setQuery("Select * from #__jhtraq_milestones where id='".(int)$id."'");
		return $this->db->$method();
	}
	
	function getPaged()
	{
		$mainframe =& JFactory::getApplication();
		$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		jimport('joomla.html.pagination');
		$search = JRequest::getVar('search');
		$project = (int)JRequest::getVar('project');
		
		$query = "Select SQL_CALC_FOUND_ROWS * from #__jhtraq_milestones";
		
		if($search != '')
		$query .= "Where (milestone like'%$search%' or slug like '%$search%' or codename like '%$search%')";
		
		if($project > 0 && $search != '')
		{
			$query .= " AND project_id='$project'";
		}elseif($project > 0 && $search == '')
		{
			$query .= " Where project_id='$project'";
		}
		
		$query .= ' ORDER BY milestone ASC';
		
		$this->db->setQuery($query,$limitstart, $limit );
		$r =&$this->db->loadAssocList();
		//print_r($this->db);
		
		$this->db->setQuery('SELECT FOUND_ROWS();');
		$this->_pagination = new JPagination( $this->db->loadResult(), $limitstart, $limit );
		
		return $r;
	}
	
	function save($post)
	{
		$id = (int)$post['id'];
		$milestone = $this->db->getEscaped($post['milestone']);
		$slug = $this->db->getEscaped($post['slug']);
		$codename = $this->db->getEscaped($post['codename']);
		$info = $this->db->getEscaped($post['info']);
		$changelog = $this->db->getEscaped(@$post['changelog']);
		$due = ($post['due'] != '')?strtotime($post['due']):0;
		$completed = $_POST['completed'];
		$cancelled = $_POST['cancelled'];
		$locked = 0;
		$displayorder = (int)$post['displayorder'];
		$project_id = (int)$post['project_id'];
		
		$status = $_POST['status'];
		if($status == 'active')
		{
			$completed = 0;
			$cancelled = 0;
			$locked = 0;
		}elseif($status == 'completed')
		{
			$locked = 1;
			$cancelled = 0;
			if((int)$completed == 0)
			$completed = strtotime("now");
		}elseif($status == 'cancelled')
		{
			$locked = 1;
			$completed = 0;
			if((int)$cancelled == 0)
			$cancelled = strtotime("now");
		}
		
		$q = "Set milestone='$milestone',slug='$slug',codename='$codename',info='$info',changelog='$changelog',due='$due',completed='$completed',cancelled='$cancelled',locked='$locked',project_id='$project_id',displayorder='$displayorder'";
		if($id > 0)
		{
			$query = "Update #__jhtraq_milestones " . $q . " Where id='$id'";
		}else{
			$query = "Insert into #__jhtraq_milestones " .$q;
		}
		$this->db->setQuery($query);
		return $this->db->query();
		
	}
	
	function remove($ids)
	{
		if(!is_array($ids))
			$ids = array((int)$ids);
		$this->db->setQuery("Delete from #__jhtraq_milestones where id in(".implode(",",$ids) . ")");
		return $this->db->query();
	}
	
	function getByProject($id)
	{
		$this->db->setQuery("SELECT * FROM #__jhtraq_milestones WHERE project_id='$id' AND cancelled='0' ORDER BY displayorder ASC");
		$info = $this->db->loadAssocList();
		foreach($info as $rid=>$row)
		{
			$i = array();
			$i['open'] = $this->countOpenTicketsPerMilestone($row['id'],$id);
			$i['closed'] = $this->countClosedTicketsPerMilestone($row['id'],$id);
			$i['total'] = $i['open']+$i['closed'];
			$i['percent'] = array(
				'open' => ($i['open'] ? JhtraqHelper::getPercent($i['open'],$i['total']): 0),
				'closed' => JhtraqHelper::getPercent($i['closed'],$i['total'])
			);
			$info[$rid]['tickets'] = $i;
		}
		return $info;
	}
	
	function countOpenTicketsPerMilestone($mid,$pid)
	{
		//	echo $mid.$pid;
		$this->db->setQuery("SELECT count(*) as cnt FROM #__jhtraq_tickets WHERE milestone_id='".$mid."' AND project_id='".$pid."' AND closed='0'");
		return $this->db->loadResult();
	}
	
	function countClosedTicketsPerMilestone($mid,$pid)
	{
		$this->db->setQuery("SELECT count(*) as cnt FROM #__jhtraq_tickets WHERE milestone_id='".$mid."' AND project_id='".$pid."' AND closed='1'");
		return $this->db->loadResult();
	}
}